import 'package:boss/color_constant.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;

class PieChart extends StatefulWidget {
  final double value;
  final double size;
  final Color color;

  const PieChart(this.value, this.size, {this.color, Key key}) : super(key: key);

  @override
  _PieChartState createState() => _PieChartState();
}

class _PieChartState extends State<PieChart> with SingleTickerProviderStateMixin {
  String get centerText => '${(widget.value * 100).toInt()}';

  AnimationController controller;
  Tween<double> tween;
  Animation<double> animation;

  @override
  void initState() {
    super.initState();

    controller = AnimationController(
      duration: Duration(milliseconds: 300),
      vsync: this,
    )..addListener(() {
        setState(() {});
      });

    Future.delayed(Duration(milliseconds: 300), () => startAni(0, widget.value));
  }

  startAni(double begin, double end) {
    tween = Tween(begin: begin, end: end);
    animation = tween.animate(controller);
    controller.forward();
  }

  @override
  void dispose() {
    controller.dispose();
    controller = null;

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final color = widget.color ?? Theme.of(context).primaryColor;
    return RepaintBoundary(
      child: Container(
        width: widget.size,
        height: widget.size,
        child: Stack(
          children: <Widget>[
            CustomPaint(
              size: Size(widget.size, widget.size),
              painter: _PieChartPaint(
                value: animation?.value ?? 0,
                color: color,
              ),
            ),
            Container(
              width: widget.size,
              height: widget.size,
              padding: EdgeInsets.all(5),
              child: Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(widget.size),
                  border: Border.all(color: color, width: 5),
                ),
                alignment: Alignment.center,
                child: RichText(
                  text: TextSpan(
                    text: centerText,
                    style: TextStyle(
                      fontSize: 20,
                      color: ColorConstant.ff33,
                      fontWeight: FontWeight.bold,
                      fontFamily: 'DINAlternate-Bold',
                    ),
                    children: [
                      TextSpan(
                        text: '%',
                        style: TextStyle(
                          color: ColorConstant.ff33,
                          fontSize: 13,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _PieChartPaint extends CustomPainter {
  final double value;
  final Color color;

  double get sweepAngle => math.pi / 180 * (360 * value);

  const _PieChartPaint({
    this.value,
    this.color,
  });

  @override
  void paint(Canvas canvas, Size size) {
    _paintArc(canvas, size);
  }

  @override
  bool shouldRepaint(_PieChartPaint oldDelegate) {
    return oldDelegate.value != value || oldDelegate.color != color;
  }

  void _paintArc(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..strokeWidth = 1
      ..style = PaintingStyle.stroke
      ..isAntiAlias = true;

    final radius = size.width / 2;
    final rect = Rect.fromCircle(center: Offset(radius, radius), radius: radius);

    canvas.drawArc(rect, math.pi / 180 * 90, sweepAngle, false, paint);
  }
}
